home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / progjour / 1988 / 01 / env.pas < prev    next >
Pascal/Delphi Source File  |  1987-12-28  |  4KB  |  147 lines

  1. {Listing 1: ENV.PAS
  2.     Page 44 Volume 6.1                                      }   
  3.  
  4. PROGRAM Env;
  5.  
  6. { Demonstrate how to read environment strings in Turbo Pascal 4.0.
  7.   (C) 1987 by Swan Software, P.O. Box 206, Lititz PA 17543 }
  8.  
  9. TYPE
  10.  
  11.    CharPtr = ^Char;   { Pointer to characters }
  12.  
  13. VAR
  14.    s : String;   { Holds environment string settings }
  15.  
  16.  
  17. PROCEDURE AdvanceCP( VAR p : CharPtr );
  18.  
  19. { Add one to p's offset address.  This procedure cannot advance
  20.   p beyond a segment boundary.  Later Turbo Pascal versions might
  21.   let you replace calls to AdvanceCP(p) with Inc(p). }
  22.  
  23. BEGIN
  24.    p := Ptr( Seg(p^), Ofs(p^) + 1 )
  25. END; { AdvanceCP }
  26.  
  27.  
  28. FUNCTION GetEnv( s : String ) : CharPtr;
  29.  
  30. { Search environment for string s and return a pointer to the
  31.   setting for this environment variable.  If the variable is not
  32.   found, return nil.  For example, if s equals 'COMSPEC', then
  33.   GetEnv returns a pointer to the current COMSPEC setting,
  34.   probably C:\COMMAND.COM.  String s is case sensitive.  In most
  35.   cases, it should be in all caps. }
  36.  
  37. VAR
  38.  
  39.    p : CharPtr;   { Pointer to environment characters }
  40.  
  41.  
  42.    FUNCTION Match( VAR s : String; VAR p : CharPtr ) : Boolean;
  43.  
  44.    { True if characters at p^ match string s exactly.  If they do
  45.      match, then p^ addresses the character after the end of the
  46.      matching string.  If they do not match, then p addresses the
  47.      character that does not match. }
  48.  
  49.    VAR
  50.  
  51.       i : Integer;   { FOR-loop control variable }
  52.  
  53.    BEGIN
  54.       FOR i := 1 TO Length(s) DO   { Examine all chars in s }
  55.       BEGIN
  56.          IF p^ <> s[i] THEN        { If mismatch found... }
  57.          BEGIN
  58.             Match := FALSE;        { ... return false }
  59.             exit                   {     and exit Match }
  60.          END; { if }
  61.          AdvanceCP( p )            { Continue looking }
  62.       END; { for }
  63.  
  64.     { All characters match.  Return true if character
  65.       at p is an equal sign. }
  66.  
  67.       Match := ( p^ = '=' )
  68.  
  69.    END; { Match }
  70.  
  71.  
  72. BEGIN { GetEnv }
  73.  
  74. { Pick up environment pointer at PSP:002C, and assign to p making
  75.   it point to the first character of the environment block. }
  76.  
  77.    p := Ptr( MemW[ PrefixSeg:$002C ], 0 );
  78.  
  79.    WHILE p^ <> Chr(0) DO    { While not at end of environment block }
  80.    BEGIN
  81.       IF Match( s, p )      { If the string s matches chars at p^ }
  82.        THEN
  83.          BEGIN
  84.             AdvanceCP(p);  { Advance p past = sign }
  85.             GetEnv := p;   { Return pointer to ASCIIZ string }
  86.             exit           { Exit procedure early }
  87.          END; { if }
  88.       WHILE p^ <> Chr(0) DO   { Find end of ASCIIZ string }
  89.          AdvanceCP( p );
  90.       AdvanceCP( p )       { Advance p to next string (if any) }
  91.    END; { while }
  92.  
  93.    GetEnv := NIL  { Environment variable not found }
  94.  
  95. END; { GetEnv }
  96.  
  97.  
  98. PROCEDURE ASCIIZtoStr( p : CharPtr; VAR s : String );
  99.  
  100. { Convert ASCIIZ character string addressed by p into a length-byte
  101.   string variable, s.  The character array must end in a null zero
  102.   byte.  The string is limited to 255 characters.  If p is NIL, then
  103.   ASCIIZtoStr returns zero for the string length. }
  104.  
  105. VAR
  106.  
  107.    count : integer;   { String length counter }
  108.  
  109. BEGIN
  110.  
  111.    count := 0;  { Initialize string length to zero }
  112.  
  113.    IF p <> NIL THEN  { Return zero length string for nil pointer }
  114.  
  115.       WHILE ( p^ <> Chr(0) ) AND ( count < 255 ) DO
  116.       BEGIN
  117.          count := count + 1;   { Count characters transferred }
  118.          s[ count ] := p^;     { Assign char at p to string s }
  119.          AdvanceCP( p )        { Advance pointer to next char }
  120.       END; { while }
  121.  
  122.    s[0] := Chr( count )       { Assign string length byte }
  123.  
  124. END; { ASCIIZtoStr }
  125.  
  126.  
  127. BEGIN
  128.    Writeln;
  129.    Writeln( 'Test reading environment strings' );
  130.    Writeln;
  131.    ASCIIZtoStr( GetEnv( 'COMSPEC' ), s );
  132.    writeln( 'COMSPEC ...... ', s );
  133.    ASCIIZtoStr( GetEnv( 'PATH' ), s );
  134.    writeln( 'PATH ......... ', s );
  135.    ASCIIZtoStr( GetEnv( 'PROMPT' ), s );
  136.    writeln( 'PROMPT ....... ', s );
  137.    ASCIIZtoStr( GetEnv( 'TEMP' ), s );
  138.    writeln( 'TEMP ......... ', s )
  139. END.
  140.  
  141. From:     Programmer's Journal / MCI ID: 336-4248
  142.  
  143. TO:       Swan Software 
  144. Subject:  RE: Article
  145.  
  146. Thank's Tom.  Looks good.  We'll be in touch shortly.  Chuck A.
  147.